<?php
/*--------------------------------------------------------------
	* Copyright (C) 2008-2010 itye.cn/myaim.cn
	*
	* 文件名:Page.php
	* 说明:分页类
	*
	* 程序员:吴锋
	* QQ:7462679
	* Email:lovq@163.com
	*
	* 最后修改时间:2010-06-09
	* 修改说明：
	*
----------------------------------------------------------------*/
if (!defined('SHARE_VERSION')) exit('No direct script access allowed');

class Page{
	public $count;	// 总数据量
	public $intShowNum = 20;	// 每页数量
	// 当前页面
	public $nowPage = 1;
	// 总页数
	public $pageNum;
	// 起始页
	public $firstRow;
	// 每页显示数量
	public $listRow;
	// 链接参数
	public $parameter;
	// 分页显示定制
    public $config  =	array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页', 'theme' => 2);

	public function __construct($count, $intShowNum=0) {
		$count      		= intval($count);
		$intShowNum 		= intval($intShowNum);
		$this->count 		= $count;
		if ($intShowNum>0) {
			$this->intShowNum 	= $intShowNum;
		}
		$this->nowPage 		= intval($this->_getNowPage());
		$this->pageNum 		= intval($this->_getPageNum());
		$this->firstRow		= ($this->nowPage - 1) * $this->intShowNum;
		$this->listRow		= $this->intShowNum;
	}
	
	// 通过get取出当前页地址
	private function _getNowPage(){
		$page		= isset($_GET['page']) ? intval($_GET['page']) : 1;
		if ( $page > $this->_getPageNum() && $this->_getPageNum() != 0 ){
			$page 	= $this->_getPageNum();
		} elseif ( $page ==0 ) {
			$page 	= 1;
		}
		return $page;
	}
	// 取得总页数
	private function _getPageNum(){
		$pageNum = ceil($this->count/$this->intShowNum);	//得到总页数
		return $pageNum;
	}
	// 设置配置
	public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }
	/*=======================================
	* 分页函数
	* @param $PageUrl	分页URL
	* @param $RsCount	数据总量
	* @param $PageCount	总页数
	* @param $Page		当前页码
	* @param $PageSize  每页显示的量
	* @param $Suffix	URL后缀
	* @param $Types		显示方式
	=========================================*/
	//function show($PageUrl, $RsCount, $PageCount, $Page, $PageSize, $Suffix, $Types)
	function show() {
		if(0 == $this->pageNum) return '';
		if (isset ( $_SERVER ['HTTP_X_REWRITE_URL'] )) {
			$_SERVER ['REQUEST_URI'] = $_SERVER ['HTTP_X_REWRITE_URL'];
		}elseif (isset ( $_SERVER ['HTTP_REQUEST_URI'] )) {
			$_SERVER ['REQUEST_URI'] = $_SERVER ['HTTP_REQUEST_URI'];
		} else {
			if (isset ( $_SERVER ['SCRIPT_NAME'] )){
				$_SERVER ['HTTP_REQUEST_URI'] = $_SERVER ['SCRIPT_NAME'];
			} else {
				$_SERVER ['HTTP_REQUEST_URI'] = $_SERVER ['PHP_SELF'];
			}
			if ($_SERVER ['QUERY_STRING']) {
				$_SERVER ['HTTP_REQUEST_URI'] .= '?' . $_SERVER ['QUERY_STRING'];
			}
			$_SERVER ['REQUEST_URI'] = $_SERVER ['HTTP_REQUEST_URI'];
		}
		$url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
		$parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params['page']);
            $url   =  $parse['path'].'?'.http_build_query($params);
        }
		//上下翻页字串
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
        $tmp 	= '';
        if ($this->config['theme'] == 1) {
        	// 只显示上一页，下一页
        	$tmp	= '<a href="'.$url.'">'.$this->config['first'].'</a>';
        	if ($upRow>0) {
        		$tmp	.= '<a href="'.$url.'&page='.$upRow.'">'.$this->config['prev'].'</a>';
        	}
        	if ($downRow <= 0) {
        		$tmp	.= '<a href="'.$url.'&page='.$downRow.'">'.$this->config['next'].'</a>';
        	}
        	$tmp 	.= '<a href="'.$url.'&page='.$this->pageNum.'">'.$this->config['last'].'</a>';
        } elseif ($this->config['theme'] == 2) {
        	$tmp	= '<a href="'.$url.'">'.$this->config['first'].'</a>';
        	if ($upRow>0) {
        		$tmp	.= '<a href="'.$url.'&page='.$upRow.'">'.$this->config['prev'].'</a>';
        	}
        	// 
        	if ($this->pageNum<=10) {
        		// 少于10页的
        		for ($n=1; $n <= $this->pageNum; $n++) { 
        			if( $n == $this->nowPage ) {
						$tmp .= '<span class="curpage">'.$n.'</span>';
					} else {
						$tmp .= '<a href="'.$url.'&page='.$n.'">'.$n.'</a>';
					}
        		}
        	} elseif ( ($this->nowPage+6)<=$this->pageNum ) {
        		// 大于10页
        		if($this->nowPage < 5) {
					for($n=1; $n<=10; $n++) {
						if( $n == $this->nowPage ) {
							$tmp .= '<span class="curpage">'.$n.'</span>';
						} else {
							$tmp .= '<a href="'.$url.'&page='.$n.'">'.$n.'</a>';
						}
					}
				} else {
					for($n=($this->nowPage-4); $n<=($this->nowPage+5); $n++){
						if( $n == $this->nowPage ) {
							$tmp .= '<span class="curpage">'.$n.'</span>';
						} else {
							$tmp .= '<a href="'.$url.'&page='.$n.'">'.$n.'</a>';
						}
					}
				}
        	} else {
				for($n=($this->nowPage-4); $n<=$this->pageNum; $n++) {
					if( $n == $this->nowPage ) {
						$tmp .= '<span class="curpage">'.$n.'</span>';
					} else {
						$tmp .= '<a href="'.$url.'&page='.$n.'">'.$n.'</a>';
					}
				}
			}
        	if ($downRow <= 0) {
        		$tmp	.= '<a href="'.$url.'&page='.$downRow.'">'.$this->config['next'].'</a>';
        	}
        	$tmp 	.= '<a href="'.$url.'&page='.$this->pageNum.'">'.$this->config['last'].'</a>';
        }
        $page_info  = $this->count."条记录&nbsp;&nbsp;".$this->nowPage."/".$this->pageNum."页";
        return $page_info.$tmp;
	}
}
?>